home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / net / parnet-3.2 / extras / parpc / amigafiles / sources / task.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  3KB  |  130 lines

  1.  
  2. /*
  3.  *  TASK.C
  4.  *
  5.  *  -Accept packets to send to network
  6.  *  -Receive data from network
  7.  *
  8.  *        ParWrite(destaddr, buf, bytes)
  9.  */
  10.  
  11. #include "defs.h"
  12.  
  13. /* par.asm */
  14. extern int ParDataReady(void);
  15. extern int ParReadV();
  16. extern int ParWriteV();
  17.  
  18. typedef struct {
  19.     uword   Port;   /*    destination port    */
  20.     uword   ChkSum; /*    data checksum        */
  21.     ulong   Bytes;  /*    # of bytes        */
  22. } Header;
  23.  
  24. char    DataBuf[MAXPKTSIZE];
  25. Header    Hdr;
  26. long    TLock[2] = { 0, 0 };
  27. static    short Cnt = 0;
  28.  
  29. /* prototyping */
  30. void CParNetTask(void);
  31. int OutputPacket(Packet *);
  32.  
  33. void
  34. CParNetTask(void)
  35. {
  36.     long mask;
  37.     Unit *unit;
  38.     Packet *packet;
  39.     long n;
  40.  
  41.     for (;;) {
  42.     mask = Wait(SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F);
  43.     LockAddr(TLock);
  44.     if (mask & SIGBREAKF_CTRL_F) {      /*  packet pending  */
  45. data:
  46.         if (ParDataReady() > 0) {
  47.         n = ParReadV(&Hdr, sizeof(Hdr), DataBuf, MAXPKTSIZE, NULL, NULL);
  48.  
  49.         sprintf(StickyPort->DebugBuf, "%03d READ %6ld bytes port %d", Cnt++,n, Hdr.Port);
  50.  
  51.         if (n >= sizeof(Hdr) && n >= sizeof(Hdr) + Hdr.Bytes) {
  52.             if (unit = FindUnitForPort(Hdr.Port)) {
  53.             packet = AllocParPacket(NULL, unit, DataBuf, Hdr.Bytes, NULL, 0);
  54.             LockAddr(unit->UnitLock);
  55.             (*packet->io_Unit->DataFunc)('r', packet, n - sizeof(Hdr));
  56.             UnlockAddr(unit->UnitLock);
  57.             }
  58.         }
  59.         }
  60.     }
  61.     if (mask & SIGBREAKF_CTRL_E) {      /*  port            */
  62.         while (packet = (Packet *)GetMsg(&DevBase->Port)) {
  63.         n = OutputPacket(packet);
  64.         if (n == -2) {
  65.             Forbid();
  66.             AddHead(&DevBase->Port.mp_MsgList, (struct Node *)packet);
  67.             Permit();
  68.             goto data;
  69.         }
  70.         }
  71.     }
  72.     UnlockAddr(TLock);
  73.     }
  74. }
  75.  
  76. int
  77. OutputPacket(packet)
  78. Packet *packet;
  79. {
  80.     long n;
  81.     Unit *unit = packet->io_Unit;
  82.  
  83.     Hdr.Port  = packet->DestPort;
  84.     Hdr.ChkSum= 0;
  85.     Hdr.Bytes = packet->DLen1 + packet->DLen2;
  86.  
  87.     n = ParWriteV(packet->DestAddr, &Hdr, sizeof(Hdr), packet->Data1, packet->DLen1, packet->Data2, packet->DLen2, NULL, NULL);
  88.  
  89.     sprintf(StickyPort->DebugBuf, "%03d WRITE %6ld to %d port %d", Cnt++,
  90.         n,packet->DestAddr,packet->DestPort);
  91.  
  92.     if (n == -2)                /*  can't write, pending rcv    */
  93.     return(n);
  94.     LockAddr(unit->UnitLock);
  95.     if (n == sizeof(Hdr) + packet->DLen1 + packet->DLen2)
  96.     (*unit->DataFunc)('w', packet, n - sizeof(Hdr));
  97.     else
  98.     (*unit->DataFunc)('W', packet, n - sizeof(Hdr));
  99.     UnlockAddr(unit->UnitLock);
  100.     return(n);
  101. }
  102.  
  103.  
  104. /*
  105.  *  Queue packet for write.  If QUICKIO is requested and the packet can be
  106.  *  sent manually it is, else it is queued to the task.
  107.  */
  108.  
  109. void
  110. QueuePacketForWrite(packet)
  111. Packet *packet;
  112. {
  113.     Iob *iob = packet->iob;
  114.  
  115.     if ((iob->io_Flags & IOF_QUICK) && TryLockAddr(TLock) > 0) {
  116.     if (OutputPacket(packet) != -2) {
  117.         UnlockAddr(TLock);
  118.         return;
  119.     }
  120.     /*
  121.      *  Can't write packet, rcv packet pending.
  122.      */
  123.     UnlockAddr(TLock);
  124.     }
  125.     iob->io_Flags &= ~IOF_QUICK;
  126.     iob->io_Flags |= IOF_QUEUED;
  127.     PutMsg(&DevBase->Port, &packet->Msg);
  128. }
  129.  
  130.